package cn.zhaoyuening.crm.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import cn.zhaoyuening.crm.dao.CourseTypeDao;
import cn.zhaoyuening.crm.domain.CrmCourseType;
import cn.zhaoyuening.crm.service.CourseTypeService;
import cn.zhaoyuening.crm.utils.page.PageBean;

public class CourseTypeServiceImpl implements CourseTypeService {
	private CourseTypeDao courseTypeDao;

	@Override
	public List<CrmCourseType> findAll(CrmCourseType courseType) {
		String hql = "";
		Object[] params = {};
		createAdvancedQueryParams(courseType, hql, params);
		List<CrmCourseType> list = courseTypeDao.findAll(hql,params);
		return list;
	}


	@Override
	public CrmCourseType findById(String id) {
		CrmCourseType courseType = courseTypeDao.findById(id);
		return courseType;
	}

	@Override
	public boolean addOrEdit(CrmCourseType courseType) {
		boolean isAdd = false;
		// 判断 add or edit
		if (StringUtils.isBlank(courseType.getCourseTypeId())) {
			// 为添加
			courseType.setCourseTypeId(null);
			isAdd = true;
		}
		this.courseTypeDao.addOrEdit(courseType);
		return isAdd;
	}

	@Override
	public PageBean<CrmCourseType> findAll(CrmCourseType courseType, int pageNum, int pageSize) {
		//1高级查询条件
		String hql = "";
		Object[] params = {};
		createAdvancedQueryParams(courseType, hql, params);
		//2分页查询
		//获取所有记录数量
		int totalRecord =(int) this.courseTypeDao.getTotalRecord();
		PageBean<CrmCourseType> pageBean = new PageBean<CrmCourseType>(pageSize, pageNum, totalRecord);
		//获取查询结果
		List<CrmCourseType> data =  this.courseTypeDao.findAll(hql, params,pageBean.getStartIndex(),pageBean.getPageSize());
		pageBean.setData(data);
		
		return pageBean;
	}
	/**
	 * 为高级查询创建查询参数
	 */
	private void createAdvancedQueryParams(CrmCourseType courseType,String hql,Object[] params){
		//创造高级查询条件语句
		StringBuilder builder = new StringBuilder();
		List<Object> objects = new ArrayList<Object>();
		
		//构造语句
		//courseName
		if (!StringUtils.isBlank(courseType.getCourseName())) {
			builder.append(" and courseName like ? ");
			objects.add("%"+courseType.getCourseName()+"%");
		}
		//remark
		if (!StringUtils.isBlank(courseType.getRemark())) {
			builder.append(" and remark like ? ");
			objects.add("%"+courseType.getRemark()+"%");
		}
		if (courseType.getCourseCostStart()!=null) {
			builder.append(" and courseCost >= ? ");
			objects.add(courseType.getCourseCostStart());
		}
		if (courseType.getCourseCostEnd()!=null) {
			builder.append(" and courseCost <= ? ");
			objects.add(courseType.getCourseCostEnd());
		}
		if (courseType.getTotalStart()!=null) {
			builder.append(" and total >= ? ");
			objects.add(courseType.getTotalStart());
		}
		
		if (courseType.getTotalEnd()!=null) {
			builder.append(" and total <= ? ");
			objects.add(courseType.getTotalEnd());
		}
		
		hql = builder.toString();
		params = objects.toArray();
	}
	
	// =====get&set=======
	
	public void setCourseTypeDao(CourseTypeDao courseTypeDao) {
		this.courseTypeDao = courseTypeDao;
	}
}
